﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SZJT.Framework.Core
{
    public static class XmlHelper
    {
        public static string GetElementString(string elementName)
        {
            return string.Concat("<", elementName, ">");
        }

        public static string GetEndElementString(string elementName)
        {
            return string.Concat("</", elementName, ">");
        }

        public static string GetAttributeString(string attributeName, string attributeValue)
        {
            return string.Concat(Constants.Blank, attributeName, "=\"", attributeValue, "\"");
        }

        public static string GetPropertyName(string propertyName)
        {
            if (propertyName == Constants.EntityPkName)
                return Constants.ServicePkName;

            return propertyName;
        }

        public class XmlElement
        {
            private IList<string[]> _attributes = new List<string[]>();

            public XmlElement(string name)
            {
                Name = name;
            }

            public string Name
            {
                get;
                set;
            }

            public XmlElement AddAttribute(string name, string value)
            {
                _attributes.Add(new string[] { name, value });

                return this;
            }

            public override string ToString()
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("<").Append(Name);

                foreach (string[] attribute in _attributes)
                {
                    sb.Append(XmlHelper.GetAttributeString(attribute[0], attribute[1]));
                }

                sb.Append(">");

                return sb.ToString();
            }
        }
    }
}
